package cn.com.css.taxInspection.common.job.task;

import cn.com.css.taxInspection.attention.entity.CaseAjxxRelation;
import cn.com.css.taxInspection.attention.entity.JaajxxStatuschangeEntity;
import cn.com.css.taxInspection.attention.service.CaseAjxxRelationService;
import cn.com.css.taxInspection.attention.service.JaajxxStatuschangeService;
import cn.com.css.taxInspection.gt3.entity.JcAjxxEntity;
import cn.com.css.taxInspection.gt3.service.JcAjxxService;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 记录积案案件状态变化数据
 */
@Component("ajStatusChangeTask")
public class AjStatusChangeTask {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Resource
	private CaseAjxxRelationService caseAjxxRelationService;
	@Resource
	private JcAjxxService jcAjxxService;
	@Resource
	private JaajxxStatuschangeService jaajxxStatuschangeService;

	public void run(String params) {
		logger.info("进入定时任务ajStatusChangeTask，处理积案案件状态变化数据定时任务开始");
		//创建一个表，包含所有积案，里面存案件状态，
		//每天定时从后台抓取数据，判断表中案件状态是否变化，有变化增加一条数据（含案件编号，案件状态，创建时间），
		// 此表设计开关，可全部清除导出。
		//1.获取所有积案 一类积案 和二类积案
		//String[] allJaCaseIds = {"1753235180390457346","4A83C7C7819843A6BC439E31E864FDDB","1753235137625333761","1753235212262973441",
		//		 "1753235156424204289","CD7144F1D8EF4F2A807B56BB6EF836C3","1753235199331934210","1753235225022046209"};
		LambdaQueryWrapper<JaajxxStatuschangeEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(JaajxxStatuschangeEntity::getYxbz,"Y");
		List<JaajxxStatuschangeEntity> allJaAjxxDataList = jaajxxStatuschangeService.list(queryWrapper);
		// 根据 createtime 倒序排序
		List<JaajxxStatuschangeEntity> sortedList = allJaAjxxDataList.stream()
				.sorted(Comparator.comparing(JaajxxStatuschangeEntity::getCreateTime).reversed())
				.collect(Collectors.toList());

		// 根据 jcajbh 去重，保留 createtime 最大的记录
		List<JaajxxStatuschangeEntity> resultList = sortedList.stream()
				.collect(Collectors.toMap(JaajxxStatuschangeEntity::getJcajbh, obj -> obj, (existing, replacement) -> existing))
				.values().stream()
				.collect(Collectors.toList());
		//2.根据获取的积案案件编号或者jcajxxuuid 去查询库查询当前案件，
		// 并判断当前案件状态与积案的案件状态如果不一致写入小助手数据库表中
		for (JaajxxStatuschangeEntity jaxx:resultList) {
			String ajbh = jaxx.getJcajbh();
			String ajztDm = jaxx.getAfterAjjcztDm();
			String swjgDm = jaxx.getSwjgDm();
			//去查询库获取当前案件在核心的数据
			JcAjxxEntity gt3Jcajxx = jcAjxxService.getByJcajbh(ajbh);
			if(TsBqUtils.isNotNull(gt3Jcajxx)){
				String gt3AjztDm = gt3Jcajxx.getAjjcztDm();
				//如果两个状态不一样，写入数据库
				if(!ajztDm.equals(gt3AjztDm)){
					JaajxxStatuschangeEntity obj = new JaajxxStatuschangeEntity();
					obj.setJcajxxuuid(gt3Jcajxx.getJcajxxuuid());
					obj.setJcajbh(ajbh);
					obj.setBeforeAjjcztDm(ajztDm);
					obj.setAfterAjjcztDm(gt3AjztDm);
					obj.setCreateTime(new Date());
					obj.setSwjgDm(swjgDm);
					jaajxxStatuschangeService.save(obj);
				}
			}
		}
		logger.info("定时任务ajStatusChangeTask，处理积案案件状态变化数据定时任务结束");
	}

}
